package com.example.clienteclinicav2;

import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ModificarRegistroActivity extends Activity {

	
	private EditText diagnosticoEditText;
	private EditText fechaEditText;
	private TextView rutTextView;
	private Spinner spinnerPrestaciones;
	private ArrayList<String> codPrestaciones  = new ArrayList<String>();
	private String codiPrest;
	private String NAMESPACE = "http://capaservicio";
	private String URL = "http://192.168.0.12:8080/ServicioPrestacionesClinica/services/PrestacionORM";
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modificar_registro);
        
        diagnosticoEditText = (EditText) findViewById(R.id.txtDiagnostico);
        rutTextView = (TextView) findViewById(R.id.txtRut);
        fechaEditText = (EditText) findViewById(R.id.txtFecha);
        spinnerPrestaciones = (Spinner) findViewById(R.id.spinnerPrestaciones);

        Bundle bundle=getIntent().getExtras();
        rutTextView.setText(bundle.getString("rut"));
        diagnosticoEditText.setText(bundle.getString("diagnostico"));
        fechaEditText.setText(bundle.getString("fecha"));
        codiPrest = bundle.getString("idPrestacion");
        new ObtenerPrestacionesBackground().execute();
    	//Log.i("registro del registro:", bundle.getString("diagnostico")+bundle.getString("fecha")+bundle.getString("rut"));
    }
	
	public void posicionSpinner(){
		for(int contar =0; contar<codPrestaciones.size();contar++){
			if(codiPrest==codPrestaciones.get(contar)){
			//	spinnerPrestaciones.setSelection(contar);
			}
		}
	}
	
	 class ObtenerPrestacionesBackground extends AsyncTask<Void, Void, String>{
		 private ProgressDialog progressDialog;
		 	@Override
			protected void onPreExecute() {
	    		progressDialog = ProgressDialog.show(
	    				ModificarRegistroActivity.this,
	    				"PrescliV1.0", 
	    				"Espere mientras se carga las prestaciones ...", 
	    				true);
				super.onPreExecute();
			}
			
			@Override
			protected String doInBackground(Void... params) {

				String resultadoJSON="";
		    	String METHOD_NAME = "obtenerListadoPrestaciones";
		    	String SOAP_ACTION = NAMESPACE +"/" +METHOD_NAME;
				SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
				SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				soapEnvelope.dotNet = true;
				soapEnvelope.implicitTypes =true;
				soapEnvelope.setOutputSoapObject(request);

				HttpTransportSE aht = new HttpTransportSE(URL);
				Log.d("Cliente SOAP", "Iniciando Consulta");
				try {
					aht.call(SOAP_ACTION, soapEnvelope);
					SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
					resultadoJSON = resultString.toString();
					Log.d("Cliente SOAP", "Finalizando Consulta:"+resultString.toString());
					
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}
		    
				return resultadoJSON;
			}

			@Override
			protected void onPostExecute(String result) {
				super.onPostExecute(result);
				progressDialog.dismiss();
				Log.d("Cliente SOAP", "ultimo mtodo");
				if("".equals(result)){
					Toast.makeText(ModificarRegistroActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
				}else{
					try {
						ArrayList<String> prestacionesList = new ArrayList<String>();
						JSONArray peliculasJSONArray = new JSONArray(result);
						int cantPrestaciones = peliculasJSONArray.length();
						for(int contar = 0; contar< cantPrestaciones; contar ++){
							Log.d("ClienteSoap", peliculasJSONArray.getJSONObject(contar).getString("idPrestacion"));
							codPrestaciones.add(peliculasJSONArray.getJSONObject(contar).getString("idPrestacion"));
							prestacionesList.add(peliculasJSONArray.getJSONObject(contar).getString("descripcion"));
						}
						
						ArrayAdapter<String> generosAdapter = new ArrayAdapter<String>(ModificarRegistroActivity.this, android.R.layout.simple_spinner_item, prestacionesList);
						spinnerPrestaciones.setAdapter(generosAdapter);
						posicionSpinner();
						
					} catch (JSONException e) {
						Toast.makeText(ModificarRegistroActivity.this, "Error al obtener la informacion necesaria", Toast.LENGTH_LONG).show();
					}
				}
	    }
	 }
}
